package com.mdd.common.entity.yangyangLeadingEnterprise.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.sql.Time;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//返回的json对象
public class JsonReturn implements Serializable {

	private int status ;//请求结果状态
	private String msg ;//返回信息
	private List listData;//列表页数据集合
	private Map map;//额外数据

	/**
	 * 推荐使用无参的 toJsonStr
	 * @param jr
	 * @return
	 */
	@Deprecated
	public String toJsonStr(JsonReturn jr){

		GsonBuilder builder=new GsonBuilder();
		builder.registerTypeAdapter(Double.class, (JsonSerializer<Double>) (aDouble, type, jsonSerializationContext) -> {
			DecimalFormat format=new DecimalFormat("##0.00");
			String temp=format.format(aDouble);
			JsonPrimitive pri=new JsonPrimitive(temp);
			return pri;
		});

		//将又拍云的图片地址头，封装进去
		Map map = jr.getMap();
		if(map==null){
			map = new HashMap();
		}
//		map.put("upyunHead", MultiUploadHandler.getUpyunUrl()+"/");
		jr.setMap(map);
		map = null;
		Gson gson = builder.create();
		String json = gson.toJson(jr,new TypeToken<JsonReturn>(){}.getType());
		gson = null;
		return json;
	}

	public static JsonReturn success(){
		JsonReturn jsonReturn = new JsonReturn();
		jsonReturn.setStatus(200);
		return jsonReturn;
	}
	public static JsonReturn success(String msg){
		JsonReturn success = success();
		success.setMsg(msg);
		return success;
	}
	public static JsonReturn fail(){
		JsonReturn jsonReturn = new JsonReturn();
		jsonReturn.setStatus(509);
		jsonReturn.setMsg("服务器内部错误");
		return jsonReturn;
	}

	public static JsonReturn fail(String msg){
		JsonReturn fail = fail();
		fail.setMsg(msg);
		return fail;
	}

	public static JsonReturn fail(String msg,int status){
		JsonReturn fail = fail();
		fail.setStatus(status);
		fail.setMsg(msg);
		return fail;
	}

	/**
	 * 转为JSON不转换 数值
	 * @return
	 */
	public String toJsonStrOrigin(){
		//将又拍云的图片地址头，封装进去
		Map map = this.getMap();
		if(map==null){
			map = new HashMap();
		}
//		map.put("upyunHead", MultiUploadHandler.getUpyunUrl()+"/");
		this.setMap(map);
		map = null;
		GsonBuilder builder=new GsonBuilder();
		Gson gson=builder.create();
		String json = gson.toJson(this,new TypeToken<JsonReturn>(){}.getType());
		gson = null;
		return json;
	}



	/**
	 * 转为JSON不转换 浮点型 去两位小数
	 * @return
	 */
	public String toJsonStr(){

		//将又拍云的图片地址头，封装进去
		Map map = this.getMap();
		if(map==null){
			map = new HashMap(16);
		}
//		map.put("upyunHead", MultiUploadHandler.getUpyunUrl()+"/");
		this.setMap(map);
		map = null;
		GsonBuilder builder=new GsonBuilder();
		builder.registerTypeAdapter(Double.class, (JsonSerializer<Double>) (aDouble, type, jsonSerializationContext) -> {
			DecimalFormat format=new DecimalFormat("##0.00");
			String temp=format.format(aDouble);
			JsonPrimitive pri=new JsonPrimitive(temp);
			return pri;
		});
		//格式化时间
		builder.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) -> {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String temp=format.format(date);
			JsonPrimitive pri=new JsonPrimitive(temp);
			return pri;
		});
		//格式化time
		builder.registerTypeAdapter(Time.class, (JsonSerializer<Date>) (time, type, jsonSerializationContext) -> {
			SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
			String temp=format.format(time);
			JsonPrimitive pri=new JsonPrimitive(temp);
			return pri;
		});
		Gson gson=builder.create();
		String json = gson.toJson(this,new TypeToken<JsonReturn>(){}.getType());
		gson = null;
		return json;
	}
	public Map getHashMap(){
		if(this.map!=null){
			return this.map;
		}
		this.map = new HashMap(16);
		return this.map;
	}

	public JsonReturn put(String key,Object value){
		if(this.map==null){
			this.map=new HashMap();
		}
		this.map.put(key,value);
		return this;
	}

//	public static  JsonReturn fromException(CommonException e){
//		JsonReturn jsonReturn = new JsonReturn();
//		jsonReturn.setStatus(e.getStatus());
//		jsonReturn.setMsg(e.getMessage());
//		jsonReturn.setMap(e.getParams());
//		return jsonReturn;
//	}


	
	
	
	
	public int getStatus() {
		return status;
	}
	public JsonReturn setStatus(int status) {
		this.status = status;
		return this;
	}
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List getListData() {
		return listData;
	}
	public JsonReturn setListData(List listData) {
		this.listData = listData;
		return this;
	}
//	public ListBean getListBean() {
//		return listBean;
//	}
//	public void setListBean(ListBean listBean) {
//		this.listBean = listBean;
//	}
	public Map getMap() {
		if(this.map==null){
			map = new HashMap();
		}
		return map;
	}
	public JsonReturn setMap(Map map) {
		this.map = map;
		return this;
	}
	public String toJsonStr(String dateFormat){
		GsonBuilder builder=new GsonBuilder();
		builder.registerTypeAdapter(Double.class, (JsonSerializer<Double>) (aDouble, type, jsonSerializationContext) -> {
			DecimalFormat format=new DecimalFormat("##0.00");
			String temp=format.format(aDouble);
			JsonPrimitive pri=new JsonPrimitive(temp);
			return pri;
		});
		builder.setDateFormat(dateFormat);
		Gson gson=builder.create();
		//将又拍云的图片地址头，封装进去
		Map map = this.getMap();
		if(map==null){
			map = new HashMap();
		}
//		map.put("upyunHead", MultiUploadHandler.getUpyunUrl()+"/");
		this.setMap(map);
		String json = gson.toJson(this,new TypeToken<JsonReturn>(){}.getType());
		return json;
	}
	
}
